package org.exez.modules.visitor.util;
import io.netty.util.internal.StringUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.exez.modules.visitor.constant.Enum;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author Administrator
 *
 */
public class AES {

    // 加密
    public static String Encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能，同时能起到2次加密的作用。
    }

    // 解密
    public static String Decrypt(String sSrc, String sKey) {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }
	public static String AESjiami(Object o) {
		String returnString = null;
		if (o != null && o.toString().length() > 0) {
			try {
				returnString = AES.Encrypt(o.toString(), Enum.SPY_AES_KEY.getName());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return returnString;
	}
	public static String CJAESjiami(Object o) {
		String returnString = null;
		if (o != null && o.toString().length() > 0) {
			try {
				returnString = AES.Encrypt(o.toString(), Enum.SPY_AES_KEY.getName());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return returnString;
	}
	public static String AESjiemi(Object o) {
		String returnString = null;
		if (o != null && o.toString().length() > 0) {
			try {
				returnString =AES.Decrypt(o.toString(), Enum.SPY_AES_KEY.getName());// AES.Encrypt(o.toString(), CommonEnum.AESKey.getName());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return returnString;
	}
	public static String CJAESjiemi(Object o) {
		String returnString = null;
		if (o != null && o.toString().length() > 0) {
			try {
				returnString =AES.Decrypt(o.toString(), Enum.SPY_AES_KEY.getName());// AES.Encrypt(o.toString(), CommonEnum.AESKey.getName());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return returnString;
	}

    /**
     * 手机号码和身份证隐藏
     * @param
     * @return
     */
    public static String mobileIdCard(String mobileIdCard) {
        String returnString = "*******";
        if(!StringUtil.isNullOrEmpty(mobileIdCard)){
            if(mobileIdCard.length() > 11){
                mobileIdCard = AESjiemi(mobileIdCard);
                if(mobileIdCard != null && mobileIdCard.length() > 11){
                    return mobileIdCard.substring(0,2)+"**************"+mobileIdCard.substring(mobileIdCard.length()-2);
                }else{
                    return mobileIdCard.substring(0,2)+"*******"+mobileIdCard.substring(mobileIdCard.length()-2);
                }
            }
        }
        return returnString;
    }

    /**
     * 手机号码和身份证隐藏
     * @param
     * @return
     */
    public static String mobileIdCardHide(String mobileIdCard) {
        String returnString = "*******";
        if(!StringUtil.isNullOrEmpty(mobileIdCard)){
                if(mobileIdCard != null && mobileIdCard.length() > 11){
                    return mobileIdCard.substring(0,2)+"**************"+mobileIdCard.substring(mobileIdCard.length()-2);
                }else{
                    return mobileIdCard.substring(0,2)+"*******"+mobileIdCard.substring(mobileIdCard.length()-2);
                }
        }
        return returnString;
    }

    /**
     * 姓名证隐藏
     * @param
     * @return
     */
    public static String nameXx(String name) {
        if(name != null){
            if(name.length() > 2){
                name =name.substring(0, 1)+xxx(name.length())+name.substring(name.length()-1);
            }else{
                name =name.substring(0, 1)+"*";
            }
        }
        return name;
    }

    public static String xxx (int length){
        String xxx = "";
        for (int i = 0; i < length-2; i++) {
            xxx += "*";
        }
        return xxx;
    }

    public static List<Object> institutionIdToList (String institutionId){
        String[] split = institutionId.split(",");
        return java.util.Arrays.asList(split);
    }

    public static List<Integer> integerList(String listStr){
        List<Integer>  integers=new ArrayList();

        if (StringUtils.isNotBlank(listStr)) {
            String[] split = listStr.split(",");
            List<String> list = Arrays.asList(split);
            integers = list.stream().map(Integer::parseInt).collect(Collectors.toList());
        }else {
            integers.add(-1);
        }
        return integers;
    }

    public static void main(String[] args) throws Exception {
        /*
         * 此处使用AES-128-ECB加密模式，key需要为16位。  ZTU9AQPYnxZ0t7in/T17d/Wv9pQ0jqPmywqGQtUc6ow=
         */
//    	System.err.println(CJAESjiami("142202198201071511"));
    	System.err.println("何雨林"+AESjiemi("0wUI0rrYD+FzcApllh/IFF5SnGXb4vuAx+gzMFJLmic="));
    	System.err.println("加密:"+AESjiami("17136394444"));
//    	System.err.println(AESjiemi("kcCQ5KQY/gNudbJkQyXfcpAvmwW0tVvHd1HZFkAscxw="));
//    	System.err.println("NDJKxaLXGNhTHYN9ww8msvcioRCD6FOXtHm71AezB8M=".length());
    	//System.err.println(CJAESjiemi("111AYDk001G/OJHhQ/PuzY7lALpgEh538cr+I2YpZTEnFCU0vc/FrGfloWjgRhY7"));
//        String cKey = "9cMf4(U3@.A$z9Y6";
//        // 需要加密的字串
//        String cSrc = "哈哈解密后的字串是解密后的字串是解密后的字串是解密后的字串是解密后的字串是";
//        System.out.println(cSrc);
//        // 加密
//        String enString = AES.Encrypt(cSrc, cKey);
//        System.out.println("加密后的字串是：" + enString+enString.length());
////
//        // 解密
//        String DeString = AES.Decrypt("W/SOLEIhFNhhRLFHPCl6Sw==", cKey);
//        System.out.println("解密后的字串是：" + DeString);
    }
}

//源代码片段来自云代码http://yuncode.net
